# # 分析“https://fanyi.baidu.com/”百度翻译页面，要求用户在控制台输入需要翻译的单词给出翻译结果
import requests


def translate_with_baidu(word):
    url = "https://fanyi.baidu.com/sug"

    # 构造请求参数
    params = {
        "kw": word
    }

    try:

        response = requests.post(url, data=params)

        json_data = response.json()

        translation = json_data['data'][0]['v']

        return translation

    except Exception as e:
        print("翻译出错:", e)
        return None


def main():
    word = input("请输入需要翻译的单词或短语：")
    translation = translate_with_baidu(word)
    if translation:
        print(f"翻译结果：{translation}")
    else:
        print("未找到翻译结果。")


if __name__ == "__main__":
    main()

